package com.useeinfo.oa.modules.maintaining.biz;

import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.CommonUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.DictInfoBiz;
import com.useeinfo.oa.modules.base.biz.ResourceBiz;
import com.useeinfo.oa.modules.base.entity.DictInfo;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.maintaining.dao.MaintainWorkLogDao;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import com.useeinfo.oa.modules.maintaining.entity.MaintainWorkLog;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * @auther: bin.liu
 * @Date: 2019/1/9 13:31
 * @Description: 维保工作日志service
 */
@Service("maintainWorkLogBiz")
public class MaintainWorkLogBiz extends CrudBiz<MaintainWorkLogDao, MaintainWorkLog> {
    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ResourceBiz resourceBiz;

    @Autowired
    private DictInfoBiz dictInfoBiz;

    @Autowired
    private MaintainOrderBiz maintainOrderBiz;


    public String getCode(String enterpriseId){
        return dao.getCode(enterpriseId);
    }

    /**
     * 添加或修改工作日志
     *
     * @param logId
     * @param logCatId
     * @param logLevelId
     * @param content
     * @param photos
     * @param maintainOrderId
     * @return
     */
    public ResultDto addLog(String logId, String logCatId, String logLevelId, String content, String photos, String maintainOrderId) {

        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        if (enterprise == null || systemUser == null) {
            ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }

        MaintainWorkLog maintainWorkLog;
        if (StringUtils.isNoneBlank(logId)) {
            maintainWorkLog = dao.findModel(StringConverters.null2Long(logId));
        } else {
            maintainWorkLog = new MaintainWorkLog();
            maintainWorkLog.setSerial(CommonUtils.getRandomNumber(6));
            maintainWorkLog.setCode(dao.getCode(StringConverters.null2String(enterprise.getId())));
        }

        maintainWorkLog.setContent(StringConverters.null2String(content));

        if (StringUtils.isNotBlank(photos)) {
            JSONArray jsonArray = JSONArray.parseArray(photos);
            if (jsonArray.size() > 0) {
                Set<Resource> resources = new HashSet<>();
                for (Object id : jsonArray) {
                    Resource resource = resourceBiz.findModel(StringConverters.null2Long(id));
                    resources.add(resource);
                }
                maintainWorkLog.setLogPhoto(resources);
            }
        }
        maintainWorkLog.setEnterprise(enterprise);
        maintainWorkLog.setSystemUser(systemUser);

        if (StringUtils.isNotBlank(logCatId)) {
            DictInfo logCat = dictInfoBiz.findModel(StringConverters.null2Long(logCatId));
            maintainWorkLog.setLogCat(logCat);
        }

        if (StringUtils.isNotBlank(logLevelId)) {
            DictInfo logLevel = dictInfoBiz.findModel(StringConverters.null2Long(logLevelId));
            maintainWorkLog.setLogLevel(logLevel);
        }

        if (StringUtils.isNotBlank(maintainOrderId)) {
            MaintainOrder maintainOrder = maintainOrderBiz.findModel(StringConverters.null2Long(maintainOrderId));
            maintainWorkLog.setMaintainOrder(maintainOrder);
        }
        addOrUpdate(maintainWorkLog);
        return ResultUtil.success();
    }

    /**
     * 工作日志列表
     *
     * @param pageIndex
     * @param pageSize
     * @param maintainOrderId
     * @return
     */
    public ResultDto maintainWorkLogList(String pageIndex, String pageSize, String maintainOrderId) {
        QueryParam queryParam = new QueryParam(pageIndex, pageSize);
        if (StringUtils.isNotBlank(maintainOrderId)) {
            queryParam.getSqlMap().put("maintainOrderId", maintainOrderId);
        }
        return ResultUtil.success(findJSONList(queryParam));
    }

    /**
     * 工作日志详情
     *
     * @param maintainWorkLogId
     * @return
     */
    public ResultDto maintainWorkLogDetail(String maintainWorkLogId) {
        if (StringUtils.isBlank(maintainWorkLogId)) {
            return ResultUtil.error("参数错误");
        }
        MaintainWorkLog maintainWorkLog = findModel(StringConverters.null2Long(maintainWorkLogId));

        return ResultUtil.success(maintainWorkLog.toJSONObjectDetail());
    }
}
